package com.feimt.config;

import com.feimt.common.JacksonObjectMapper;
import com.feimt.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @author 宋孟林
 * @description
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    //修改消息转换器
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0, converter);
    }

    /**
     * 配置静态资源映射路径
     * 该方法将特定的 URL 请求路径映射到项目中的静态资源目录，
     * 使客户端可以直接通过 URL 访问这些静态资源文件。
     * 映射规则：
     * 1. 请求路径以 "/back/**" 开头的资源，将从 "classpath:/back/" 目录中加载。
     * 2. 请求路径以 "/show/**" 开头的资源，将从 "classpath:/show/" 目录中加载。
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/back/**").addResourceLocations("classpath:/back/");
        registry.addResourceHandler("/show/**").addResourceLocations("classpath:/show/");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
    }
}
